﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SurvivalStrategy.Modules;
using SurvivalStrategy.Modules.ActionArgs;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace SurvivalStrategy
{
    //Kontroler gracza, tutaj requesty beda wysylane w zaleznosci od wejscia z klawiatury i myszki
    public class PlayerController : EntityController
    {
        KeyboardState keyboardState;
        private static World world;
        private static EntitiesManager entitiesManager;

        public PlayerController()
        {
         
        }

        public override void Update()
        {
            KeyboardState kState = Keyboard.GetState();
          
            if (keyboardState.IsKeyDown(Keys.Left))
                SendRequest(this.ID, this.ID, ActionTags.Moving, new StandardMovingArgs(new Microsoft.Xna.Framework.Vector3(-1, 0, 0)), 10, 0);
            if (keyboardState.IsKeyDown(Keys.Right))
                SendRequest(this.ID, this.ID, ActionTags.Moving, new StandardMovingArgs(new Microsoft.Xna.Framework.Vector3(1, 0, 0)), 10, 0);
            if (keyboardState.IsKeyDown(Keys.Up))
                SendRequest(this.ID, this.ID, ActionTags.Moving, new StandardMovingArgs(new Microsoft.Xna.Framework.Vector3(0, 0, -1)), 10, 0);
            if (keyboardState.IsKeyDown(Keys.Down))
                SendRequest(this.ID, this.ID, ActionTags.Moving, new StandardMovingArgs(new Microsoft.Xna.Framework.Vector3(0, 0, 1)), 10, 0);

            WorldData.WorldElement element = null;
            foreach(WorldData.WorldElement worldElement in this.GetWorldElementRelativePosition(Vector3.Zero))
            {
                if (worldElement.GetComponent<WorldData.ItemComponents.Eatable>() != null)
                    element = worldElement;
            }
            if (kState != keyboardState)
            {
                if (keyboardState.IsKeyDown(Keys.X))
                    if (element != null)
                        SendRequest(this.ID, this.ID, ActionTags.Interaction, new GetItemModuleArgs(element));
            } 
            keyboardState = kState;
            Game1.Terminal.AddText(entitiesManager.GetEntity(this.ID).GetComponent<SurvivalStrategy.Entities.EnitityComponents.SurvivalComponent>().Energy.ToString());
        }

        public static void SetWorld(World w)
        {
            if (world == null)
                world = w;
        }

        public static void SetValues(SurvivalStrategy.EntitiesManager eManager)
        {
            if (entitiesManager == null)
                entitiesManager = eManager;
        }

        public List<WorldData.WorldElement> GetWorldElementAbsolutePosition(Vector3 position)   //bierze element z danego pola
        {
            return world.GetPositionsElements(position);
        }

        public List<WorldData.WorldElement> GetWorldElementRelativePosition(Vector3 position)   //bierze element w odleglosci o jakas od naszej
        {
            return world.GetPositionsElements(Vector3.Add(GetPosition(), position));
        }

        protected Vector3 GetPosition()
        {
            return entitiesManager.GetEntity(this.ID).WorldInfo.Position;
        }
    }
}
